#include "boost/filesystem/convenience.hpp" 
namespace fs = boost::filesystem;

#include <cstdlib>
#include <string>
#include <SDL.h>
#include "tetris_app.h"
#include "slak/glui/rgbpic.h"

using namespace slak::tetris;
using namespace slak::glui;

const std::string TetrisApp::fontname = "media/fonts/TouristTrap.ttf";

TetrisApp::TetrisApp(int w,int h)
	: tetris(), soundm(), game_screen(&tetris), 
	hiscore_screen(&tetris.hi_scores, GOTO_ROOT,fontname),
	root(), slak::glui::App(w,h,&root)
{
	fs::path conf_dir;

#ifdef __APPLE__
	// portable_posix_name
	conf_dir = fs::path(getenv("HOME")) / fs::path("Library/Slak/Tetris/",fs::portable_posix_name);
#else
#  ifdef WINDOWS
	// on windows $APPDATA/Slak/Tetris
	// windows_name
	conf_dir = fs::path(getenv("APPDATA")) / fs::path("Slak/Tetris/",fs::windows_name);
#  else
	
	// on posix make ~/.slak/tetris
	// portable_posix_name
	conf_dir = fs::path(getenv("HOME")) / fs::path(".slak/tetris/",fs::portable_posix_name);
#  endif

#endif
	fs::create_directories(conf_dir);

	tetris.hi_scores.load(conf_dir / "hiscores");
	tetris.setSoundManager(&soundm);
	tetris.newGame();

	hiscore_screen.overlays.insert(hiscore_screen.overlays.begin(), new slak::glui::RGBPic("media/bitmaps/tetris-hiscore.bmp"));
	
	loop(TICK,NULL,NULL,Game::tick_delay);
}

bool TetrisApp::handle(SDL_Event& ev)
{
	switch(ev.type) {
	case SDL_USEREVENT:
		switch(ev.user.code) {
		case GOTO_HISCORES:
			setScreen(&hiscore_screen);
			return true;
		case NEW_GAME:
			tetris.newGame();
			// fall through to resume
		case RESUME:
			if (tetris.state != GAME_OVER_STATE)
				setScreen(&game_screen);
			return true;
		case GOTO_ROOT:
			setScreen(&root);
			return true;
		}
	}

	return slak::glui::App::handle(ev);
}

